<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="菜品名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="菜品分类" prop="code">
        <template>
          <el-select v-model="ruleForm.categoryId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="菜品价格" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="口味做法配置" prop="flavors">
        <el-button icon="el-icon-plus" type="primary">添加口味</el-button>
      </el-form-item>
      <el-form-item label="菜品图片" prop="image">
        <el-upload
          class="avatar-uploader"
          :action="`${env}/common/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="ruleForm.image"
            :src="`${env}/common/download?name=${ruleForm.image}`"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="菜品描述" prop="description">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="description"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >保存</el-button
        >
        <el-button @click="$router.push({ path: '/dish' })">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addDish, getDishById, updateDish } from "@/api/dish";
export default {
  computed: {
    isEdit() {
      return this.$route.query.isEdit;
    },
  },
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      //select选择器
      options: [],
      textarea: "",
      ruleForm: {
        categoryId: "",
        code: "",
        description: "",
        flavors: [{ name: "", value: [], showOption: false }],
        image: "",
        name: "",
        price: "",
        status: 1,
      },
      rules: {
        name: [{ required: true, message: "请输入菜品名称", trigger: "blur" }],
        code: [{ required: true, message: "请选择菜品分类", trigger: "blur" }],
        price: [{ required: true, message: "请设置菜品价格", trigger: "blur" }],
      },
    };
  },
  created() {
    if (this.isEdit) {
      getDishById(this.$route.query.id).then((res) => {
        this.ruleForm = res.data;
      });
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            updateDish(this.ruleForm).then((res) => {
              this.$message.success("编辑成功");
              this.$router.push({ path: "/dish" });
            });
          } else {
            addDish(this.ruleForm).then((res) => {
              this.$message.success("添加成功");
              this.$router.push({ path: "/dish" });
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  }
};
</script>

<style lang="scss" scoped>
.el-form {
  margin-top: 50px;
  width: 500px;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}

.el-icon-arrow-down {
  font-size: 12px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
